package com.suixun.service.credits.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.suixun.dao.CompanyMapper;
import com.suixun.dao.GoodsArchivesAuditRecordMapper;
import com.suixun.dao.GoodsArchivesMapper;
import com.suixun.dao.JfGoodsArchivesMapper;
import com.suixun.dao.PutAreaMapper;
import com.suixun.dao.credits.GoodsArchivesDao;
import com.suixun.entity.JfGoodsArchives;
import com.suixun.entity.JfGoodsArchivesExample;
import com.suixun.entity.credits.GoodsArchives;
import com.suixun.entity.credits.GoodsArchivesAuditRecord;
import com.suixun.entity.credits.GoodsArchivesExample;
import com.suixun.entity.credits.PutArea;
import com.suixun.entity.ext.credits.company.CompanyGoodsPage;
import com.suixun.entity.ext.credits.company.CountGoodsInfo;
import com.suixun.entity.ext.credits.company.GoodsArchivesPage;
import com.suixun.entity.ext.credits.company.GoodsPage;
import com.suixun.entity.form.credits.BasePageForm;
import com.suixun.entity.form.credits.company.GoodsArchivesPageForm;
import com.suixun.entity.form.credits.company.GoodsPageForm;
import com.suixun.service.credits.GoodsArchivesService;
import com.suixun.util.Utils;
import com.suixun.vo.credits.BaseVo;
import com.suixun.vo.credits.GoodsArchivesDetailsVo;
import com.suixun.vo.credits.GoodsArchivesVo;
import com.suixun.vo.credits.ResponseVo;

/**
 * 商品档案
 * 
 * @author yh
 * @date 2017年12月13日
 */
@Service
public class GoodsArchivesServiceImpl implements GoodsArchivesService {

	@Autowired
	private GoodsArchivesMapper goodsArchivesMapper;

	@Autowired
	private GoodsArchivesAuditRecordMapper auditRecordMapper;

	@Autowired
	private PutAreaMapper putAreaMapper;

	@Autowired
	private GoodsArchivesDao goodsArchivesDao;

	@Autowired
	private CompanyMapper companyMapper;
	@Autowired
	private JfGoodsArchivesMapper jfGoodsArchivesMapper;

	@Override
	public ResponseVo<GoodsArchivesPage> findPage(GoodsArchivesPageForm param) {
		Integer total = goodsArchivesDao.findPageTatal(param);
		List<GoodsArchivesPage> list = null;
		if (total > 0) {
			list = goodsArchivesDao.findPage(param);
		}
		return new ResponseVo<>(list, total);
	}

	@Override
	@Transactional
	public void insert(GoodsArchivesVo param, Integer userid) throws Exception {
		// 创建商品
		GoodsArchives archives = createGoodsArchives(param);
		goodsArchivesMapper.insertSelective(archives);
		// 增加审核记录
		auditRecordMapper.insertSelective(createRecord(archives, userid));

		// 投放区域
		String areaId = param.getAreaId();
		Integer putRange = param.getPutRange();
		if (putRange != null && areaId != null) {
			PutArea area = new PutArea();
			area.setAreaId(areaId);
			area.setCreateTime(new Date());
			area.setPutRange(putRange.shortValue());
			area.setGoodsArchivesId(archives.getId());
			putAreaMapper.insertSelective(area);
		}

	}

	@Override
	public GoodsArchivesDetailsVo findDetails(Integer id) throws Exception {
		GoodsArchives archives = goodsArchivesMapper.selectByPrimaryKey(id);
		GoodsArchivesDetailsVo vo = null;
		if (archives != null) {
			vo = new GoodsArchivesDetailsVo();
			BeanUtilsBean.getInstance().getConvertUtils().register(new SqlDateConverter(null), Date.class);
			BeanUtils.copyProperties(vo, archives);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			vo.setEndTime(sdf.format(archives.getEndTime()));
			vo.setBeginTime(sdf.format(archives.getBeginTime()));
			vo.setNum(archives.getTotalNum());
			String companyName = companyMapper.selectByPrimaryKey(archives.getCompanyId()).getName();
			vo.setCompanyName(companyName);
		}
		return vo;
	}

	@Override
	@Transactional
	public void update(GoodsArchivesVo param) throws Exception {
		// 修改商品
		GoodsArchives archives = goodsArchivesMapper.selectByPrimaryKey(param.getId());
		if (archives != null) {
			param.setNum(null);
			param.setUuid(null);
			BeanUtils.copyProperties(archives, param);
			if (param.getIsTop() != null && param.getIsTop()) {
				archives.setUp(new Date());
			}
			goodsArchivesMapper.updateByPrimaryKeySelective(archives);
		}

	}

	@Override
	public void delete(Integer id) {
		GoodsArchives archives = new GoodsArchives();
		archives.setId(id);
		archives.setStatus((short) 2);
		goodsArchivesMapper.updateByPrimaryKeySelective(archives);
	}

	@Override
	@Transactional
	public void auditArchives(String opinion, Boolean isPass, Integer id, Integer userid) throws Exception {
		GoodsArchives archives = goodsArchivesMapper.selectByPrimaryKey(id);
		GoodsArchivesAuditRecord record = new GoodsArchivesAuditRecord();
		BeanUtilsBean.getInstance().getConvertUtils().register(new SqlDateConverter(null), Date.class);
		BeanUtils.copyProperties(record, archives);
		record.equals(opinion);
		record.setAuthUserId(userid);
		if (isPass) {
			record.setState((short) 1);
			archives.setAuditStatus((short) 2);
		} else {
			record.setState((short) 2);
			archives.setAuditStatus((short) 3);
		}
		record.setId(null);
		goodsArchivesMapper.updateByPrimaryKeySelective(archives);
		record.setNote(opinion);
		auditRecordMapper.insertSelective(record);
	}

	@Override
	public ResponseVo<CompanyGoodsPage> findPageByCompany(BasePageForm param, Integer companyid) {
		Integer total = goodsArchivesDao.findPageByCompanyTatal(param, companyid);
		List<CompanyGoodsPage> list = null;
		if (total > 0) {
			list = goodsArchivesDao.findPageByCompany(param, companyid);
		}
		return new ResponseVo<>(list, total);
	}

	@Override
	public CountGoodsInfo countGoodsInfo(Integer archivesid) {
		return goodsArchivesDao.countGoodsInfo(archivesid);
	}

	@Override
	public ResponseVo<GoodsPage> findGoodsPage(GoodsPageForm param) {
		Integer total = goodsArchivesDao.findGoodsPageTatal(param);
		List<GoodsPage> list = null;
		if (total > 0) {
			list = goodsArchivesDao.findGoodsPage(param);
		}
		return new ResponseVo<>(list, total);
	}

	@Override
	public List<BaseVo> findAllName() {
		GoodsArchivesExample goods = new GoodsArchivesExample();
		goods.createCriteria().andStatusEqualTo((short) 1);
		List<GoodsArchives> list = goodsArchivesMapper.selectByExample(goods);
		List<BaseVo> voList = null;
		if (!CollectionUtils.isEmpty(list)) {
			voList = Lists.transform(list, new Function<GoodsArchives, BaseVo>() {
				@Override
				public BaseVo apply(GoodsArchives pojo) {
					return new BaseVo(pojo.getId(), pojo.getName());
				}
			});
		}
		return voList;
	}

	private GoodsArchivesAuditRecord createRecord(GoodsArchives archives, Integer userid) throws Exception {
		GoodsArchivesAuditRecord record = new GoodsArchivesAuditRecord();
		BeanUtils.copyProperties(record, archives);
		record.setId(null);
		record.setAuthUserId(userid);
		record.setState((short) 1);
		record.setNote("此商品为运营后台创建");
		return record;
	}

	private GoodsArchives createGoodsArchives(GoodsArchivesVo param) throws Exception {
		GoodsArchives archives = new GoodsArchives();
		BeanUtils.copyProperties(archives, param);
		archives.setId(null);
		archives.setUuid(Utils.getNextOrderSn());
		archives.setType((short) 1);
		archives.setAuditStatus((short) 2);
		Date date = new Date();
		archives.setAuditTime(date);
		// archives.setUp(date);
		Integer num = param.getNum();
		archives.setTotalNum(num);
		archives.setActualNum(num);
		archives.setAvailableNum(num);
		archives.setStatus((short) 1);
		archives.setUsetimes(1);
		archives.setCreateTime(date);
		if (param.getIsTop() != null && param.getIsTop()) {
			archives.setUp(date);
		}
		return archives;
	}

	@Override
	public List<JfGoodsArchives> listGoodsArchives(String goodsNumbers) {
		if (StringUtils.isBlank(goodsNumbers)) {
			return null;
		}
		String[] goodsNumberArray = goodsNumbers.split(",");
		List<String> goodsNumberList = new ArrayList<>();
		for (String goodsNumber : goodsNumberArray) {
			goodsNumberList.add(goodsNumber);
		}
		JfGoodsArchivesExample jfGoodsArchivesExample = new JfGoodsArchivesExample();
		jfGoodsArchivesExample.createCriteria().andUuidIn(goodsNumberList).andStatusEqualTo((short) 1)
				.andAuditStatusEqualTo((short) 2);
		return jfGoodsArchivesMapper.selectByExample(jfGoodsArchivesExample);
	}

	@Override
	public void top(Integer goodsArchivesId, Boolean isTop) {
		if (isTop == null || isTop) {
			GoodsArchives goodsArchives = new GoodsArchives();
			goodsArchives.setId(goodsArchivesId);
			Date date = new Date();
			goodsArchives.setUp(date);
			goodsArchivesMapper.updateByPrimaryKeySelective(goodsArchives);
		} else {
			goodsArchivesDao.cancelUp(goodsArchivesId);
		}
	}

}
